#include "optionWidget.h"

optionWidget::optionWidget(QWidget *parent) : QWidget(parent)
{
    //子组件的初始化
    m_CameraMiddle = new cameraMiddleWidget();
    m_CameraFrame = new optionItemFrame<cameraMiddleWidget>("相机", ":/down.png", ":/up.png", m_CameraMiddle);
    connect(m_CameraMiddle, &cameraMiddleWidget::ItemTrack, this, [=](bool track) {emit setSignalValue(signalKind::Track, (int)track); });
    m_CameraNumSetting = new CameraNumSetting();
    m_CameraNumSettingFrame = new optionItemFrame<CameraNumSetting>("相机参数设置", ":/down.png", ":/up.png", m_CameraNumSetting);
    connect(m_CameraNumSetting, &CameraNumSetting::senderValue, this, [=](signalKind kind, int value){
       setSignalValue(kind, value);
    });
    m_ShowSetting = new ShowSetting();
    m_ShowSettingFrame = new optionItemFrame<ShowSetting>("显示设置", ":/down.png", ":/up.png", m_ShowSetting);
    connect(m_ShowSetting, &ShowSetting::senderValue, this, [=](signalKind kind, int value){
       setSignalValue(kind, value);
    });
    m_VideoImage = new VideoImage();
    m_VideoImageFrame = new optionItemFrame<VideoImage>("场景捕捉", ":/down.png", ":/up.png", m_VideoImage);
    connect(m_VideoImage, &VideoImage::senderValue, this, [=](signalKind kind, int value){
       setSignalValue(kind, value);
    });

    m_SpaceWidget = new QWidget();

    m_VLayout = new QVBoxLayout(this);
    m_VLayout->addWidget(m_CameraFrame);
    m_VLayout->addWidget(m_CameraNumSettingFrame);
    m_VLayout->addWidget(m_ShowSettingFrame);
    m_VLayout->addWidget(m_VideoImageFrame);
    m_VLayout->addWidget(m_SpaceWidget);
    m_VLayout->setMargin(0);

    m_VisibleWidget = new QWidget();
    m_VisibleWidget->setLayout(m_VLayout);

    //滚动条的初始化
    m_Scroll = new QScrollArea();
    m_Scroll->setStyleSheet(QSS::QScrollArea);
    m_Scroll->setWidget(m_VisibleWidget);
    m_Scroll->setFixedWidth(340);
    //将其设置为true保证内部控件的完整性，使得滚动区域视内部控件而定
    m_Scroll->setWidgetResizable(true);

    //optionWidget整体布局
    m_HLayout = new QHBoxLayout(this);
    m_HLayout->addWidget(m_Scroll);
    m_HLayout->setMargin(0);

    m_Frames.append(qMakePair(m_CameraFrame          , optionKind::CameraKind));
    m_Frames.append(qMakePair(m_CameraNumSettingFrame, optionKind::CameraKind));
    m_Frames.append(qMakePair(m_ShowSettingFrame     , optionKind::ShowKind));
    m_Frames.append(qMakePair(m_VideoImageFrame      , optionKind::ShowKind));

    //绑定camera中心部件的更换摄像头信号与转发信号的槽函数
    connect(m_CameraFrame->RMiddleWidget(), SIGNAL(cameraCheck(const QString&)), this, SLOT(sendercameraChange(const QString &)));
    //绑定刷新按钮的事件
    connect(m_CameraFrame->RMiddleWidget(), SIGNAL(flushCamera()), this, SLOT(senderFlushCamera()));
}

//对该类型进行判断，符合的就显示不符合调用对应函数进行隐藏
bool optionWidget::setItemShow(optionKind menuBtn)
{
    for(auto e : m_Frames)
    {
        if(e.second == menuBtn)
        {
            e.first->setVisible(true);
        }
        else
        {
            e.first->setVisible(false);
        }
    }
    return true;
}

int optionWidget::optionWidth(ParameterKind kind, int width)
{
    if(!width)
    ELOG("中心框架尝试修改错误值0");
    if(kind == ParameterKind::Set && width != 0)
    {
        m_OptionWidgetWidth = width;
        this->setFixedWidth(m_OptionWidgetWidth);
        for(auto e : m_Frames)
        {
            e.first->middleWidth(ParameterKind::Set, m_OptionWidgetWidth);
        }
    }
    return m_OptionWidgetWidth;
}

bool optionWidget::setBackgroundColor(const QColor &backgroundColor, const QColor &fontColor)
{
    QPalette palette;
    palette.setBrush(QPalette::Background, backgroundColor);
    palette.setColor(QPalette::WindowText, fontColor);
    this->setPalette(palette);
    m_SpaceWidget->setPalette(palette);
    m_VisibleWidget->setPalette(palette);
    for(auto e : m_Frames)
    {
        e.first->setPaletteBF(backgroundColor, fontColor);
    }

    return true;
}

void optionWidget::optionLanguageInit()
{
    QVector<QString> pollDownLabelS;
    pollDownLabelS.push_back(tr("相机"));
    pollDownLabelS.push_back(tr("相机参数设置"));
    pollDownLabelS.push_back(tr("显示设置"));
    pollDownLabelS.push_back(tr("场景捕捉"));
    for(int i = 0; i < m_Frames.size(); i++)
    {
        m_Frames.at(i).first->middleWidgetLanguageInit();
        m_Frames.at(i).first->setPollDownText(pollDownLabelS[i]);
    }
}

cameraMiddleWidget *optionWidget::RCameraWidget()
{
    return m_CameraFrame->RCurrentMiddWidget();
}

void optionWidget::sendercameraChange(const QString &text)
{
    emit cameraChange(text);
}

void optionWidget::senderFlushCamera()
{
    emit this->flushCamera();
}
